package com.lzq.leetcode.hoot100;

import java.util.*;

public class s49 {

    public List<List<String>> groupAnagrams(String[] strs) {

        List<List<String>> ret = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        for (String str : strs) {
            char[] cs = str.toCharArray();
            Arrays.sort(cs);
            // 重新组合
            String sort = String.copyValueOf(cs);
            if (map.containsKey(sort)) {
                Integer i = map.get(sort);
                // 添加
                ret.get(i).add(str);
            } else {

                ret.add(new ArrayList<>());
                int last = ret.size()-1;
                ret.get(last).add(str);
                map.put(sort, last);

            }

        }
        return ret;
    }
}
